// index.js
import Toast from '../../miniprogram/miniprogram_npm/@vant/weapp/toast/toast';
const request = require('../../util/request.js')
const navBarHeight = wx.getSystemInfoSync().statusBarHeight + 44;
Page({
    data: {
        navBarHeight: navBarHeight,
        customer: {},
        projects: [],
        activeTab: 0,
        account: {},
        all: [],
        tabIndex: 0,
        roleslist:["","超级管理员","管理员","生产负责人","质检员","操作工"],
        user_roles:"",
        accounts: [],
        taplock: false,
        longtap: false,
        selstart:0,//初始默认选中待办事项1
        userinfo:{
          // name:"崔贺",work:"经理",group:"it",root:"1"
        },
        //质检通知
        worklist:[],
        data1:[],
        data2:[],
        data3:[],
        data4:[],
        data5:[],
    },
    setTabIndex(e) {
      console.log("onload")

        this.setData({
            tabIndex: e.currentTarget.dataset.index
        })
    },
    goPage(event) {
      console.log("onload")

        if (event.currentTarget.dataset.pageurl == "/pages/register/register") {
            Toast('暂未开放注册~');
        }

        wx.navigateTo({
            url: event.currentTarget.dataset.pageurl,
        })
    },
    testshow() {
    },
    getAccounts() {
      console.log("onload")

        var that = this;
        request.requestAjax('userinfo', {}, "加载中", function(res) {
            if (res.status == 200) {
                that.setData({
                    all: res.data.all,
                    accounts: res.data.accounts,
                });
                console.log("getAccounts")
                return false;
            } else {
                wx.showToast({
                    title: '请求失败',
                    icon: "none"
                })
            }
        console.log(this.data.selstart,"selstartselstart")

        }, function(res) {
            wx.showToast({
                title: '请求失败',
                icon: "none"
            })
        });
    },
    onLoad() {
      //默认数据,后续修改
      console.log("onload")
      var that = this
      request.requestAjax('userinfo', {}, "加载中", function(res) {
        if (res.status == 200) {
            that.setData({
                userinfo:res.data,
                user_roles:that.data.roleslist[res.data.role]
            });
            that.tosel()
            return false;
        } else {
            wx.showToast({
                title: '请求失败',
                icon: "none"
            })
        }
      }, function(res) {
          wx.showToast({
              title: '请求失败',
              icon: "none"
          })
      });
    },
    onShow() {
      console.log("onshow")
        var that = this
        request.requestAjax('userinfo', {}, "加载中", function(res) {
          if (res.status == 200) {
              that.setData({
                userinfo:res.data,
                workname:that.data.roleslist[res.data.role],
                user_roles:that.data.roleslist[res.data.role]
              });
              // return false;
          } else {
              wx.showToast({
                  title: '请求失败',
                  icon: "none"
              })
          }
    
        }, function(res) {
            wx.showToast({
                title: '请求失败',
                icon: "none"
            })
        });

        this.setData({ activeTab: this.data.activeTab })
        this.getAccounts();
        this.tosel(this.data.activeTab)
        console.log(this.data,"首页信息")
        this.setData({
         
        })
    },
    onReady() {

    },
    beforeGoPage(e) {
        var type = e.currentTarget.dataset.type;
        var index = e.currentTarget.dataset.index;
        var data = this.data.accounts[type][index];
        var jumpParam = {
            currentTarget: {
                dataset: {
                    pageurl: "/pages/accountinfo/accountinfo?id=" + data.id
                }
            }
        }


        if (data.sub_type == 4 || data.sub_type) {
            if (data.sub_type == 4) {
                //应收
                jumpParam = {
                    currentTarget: {
                        dataset: {
                            pageurl: "/pages/receivable/receivable?id=" + data.id + '&type=1'
                        }
                    }
                }
            }
            if (data.sub_type == 5) {
                //应付
                jumpParam = {
                    currentTarget: {
                        dataset: {
                            pageurl: "/pages/receivable/receivable?id=" + data.id + '&type=2'
                        }
                    }
                }
            }

        }
        this.goPage(jumpParam)
    },
    goPage(event) {
        if (event.currentTarget.dataset.pageurl == "/pages/register/register") {
            Toast('暂未开放注册~');
            return false;
        }

        wx.navigateTo({
            url: event.currentTarget.dataset.pageurl,
        })

    },

    todingdan(event) {
        wx.navigateTo({ url: '/pages/add_dingdan/add_dingdan' })
    },


    dingdanlist(event) {
        wx.navigateTo({ url: '/pages/dingdanList/dingdanList' })
    },

    shengchan(event) {
        wx.navigateTo({ url: '/pages/shengchan/shengchan' })
    },

    bianjidingdan(event) {
        wx.navigateTo({ url: '/pages/addinfo/addinfo' })
    },

    wanshandingdna(event) {
        wx.navigateTo({ url: '/pages/wanshandingdna/wanshandingdna' })
    },
    kehuinfo(event) {
        wx.navigateTo({ url: '/pages/kehuinfo/kehuinfo' })
    },
    material_management(){
      wx.navigateTo({ url: '/pages/material_management/material_management' })
    },
    pic_no_list(){
      wx.navigateTo({ url: '/pages/pic_no_list/pic_no_list' })
    },
    flow_chart(){
      wx.navigateTo({ url: '/pages/flow_chart/flow_chart' })
    },
    tosel(e){
      let that = this
      if(e === undefined){
        this.setData({
          selstart:1
        })
      }else{
        this.setData({
          selstart:Number(e)  + 1
        })
      }
      request.requestGet('get_todo_list', {}, '正在加载',function (res) {
        if (res.status === 200) {
          that.setData({
            data1:res.data.data1,
            data2:res.data.data2,
            data3:res.data.data3,
            data4:res.data.data4,
            data5:res.data.data5,
          })
          if(that.data.selstart === 1){
            that.setData({
              data1:that.data.data1,
              worklist:that.data.data1,
            });
          }else if(that.data.selstart === 2){
            that.setData({
              data2:that.data.data2,
              worklist:that.data.data2,

            });
          }else if(that.data.selstart === 3){
            that.setData({
              data3:that.data.data3,
              worklist:that.data.data3,

            });
          }else if(that.data.selstart === 4){
            that.setData({
              data4:that.data.data4,
              worklist:that.data.data4,

            });
          }else if(that.data.selstart === 5){
            that.setData({
              data5:that.data.data5,
              worklist:that.data.data5,
            });
          }
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none'
          })
        }
  
      }, function (res) {
        wx.showToast({
          title: '加载失败',
          icon:'none'
        })
      })

      // if(this.data.selstart === 1){
      //   console.log("11111111111111")
      //   console.log(that.data.data1,"11111111111111")
      //   this.setData({
      //     worklist:that.data.data1,
      //   });
      // }else if(this.data.selstart === 2){
      //   this.setData({
      //     worklist:that.data.data2,
      //   });
      // }else if(this.data.selstart === 3){
      //   this.setData({
      //     worklist:that.data.data3
      //   });
      // }else if(this.data.selstart === 4){
      //   this.setData({
      //     worklist:that.data.data4,
      //   });
      // }else if(this.data.selstart === 5){
      //   this.setData({
      //     worklist:that.data.data5,
      //   });
      // }
    },
    quality_testing(e){
      let id = e.currentTarget.dataset.index
      this.data.selstart = 1
      wx.navigateTo({ url: `/pages/quality_testing/quality_testing?id=${id}` })
    },
    new_look_purchase_orders(e){
      this.data.selstart = 4
      let id = e.currentTarget.dataset.index
      wx.navigateTo({ url: `/pages/look_purchase_orders/look_purchase_orders?id=${id}` })
    },
    go_new_look_purchase_orders(e){
      let id = e.currentTarget.dataset.index
      wx.navigateTo({ url: `/pages/new_look_purchase_orders/new_look_purchase_orders` })
    },
    handle_invalid(e){
      let id = e.currentTarget.dataset.index
      this.data.selstart = 5
      wx.navigateTo({ url: `/pages/handle_invalid/handle_invalid?id=${id}` })
    },

    setgocome(e){
      let id = e.currentTarget.dataset.index
      this.data.selstart = 3

      wx.navigateTo({  url: `/pages/setgocome/setgocome?id=${id}` })

    },
    delivery_warning(e){
        let p_no = e.currentTarget.dataset.index
        this.data.selstart = 2
        wx.navigateTo({  url: `/pages/to_delivery/to_delivery?p_no=${p_no}` })
    },
    tmpfunction(){
      let tmp = 4
      wx.navigateTo({  url: `/pages/production_record/production_record?id=${tmp}` })
    },
    flow_chart(e){
      let tmpid = e.currentTarget.dataset.id
      wx.navigateTo({ url: `/pages/flow_chart/flow_chart` })
    },

    onTabChange(event){
      // if(event.detail.index === 1){
      //   this.data.datastatus = 2
      // }else if(event.detail.index === 2){
      //   this.data.datastatus = 3
      // }else if(event.detail.index === 0){
      //   this.data.datastatus = 1
      // }else if(event.detail.index === 3){
      //   this.data.datastatus = 4
      // }else if(event.detail.index === 4){
      //   this.data.datastatus = 5
      // }
      // console.log(this.data.datastatus,"this.data.datastatus")
      this.setData({
        activeTab:event.detail.index
      })
      // this.data.page = 1
      this.tosel(this.data.activeTab,"activeTabactiveTab")
      // wx.pageScrollTo({
      //   scrollTop: 0,
      //   duration: 0
      // })
    },

    scanCode(e){
      console.log(e,"用户点击扫码")
      wx.scanCode({
        onlyFromCamera: false, // 允许从相册选择
        scanType: ['qrCode'],  // 仅识别二维码
        success: (res) => {
          console.log(res,"成功")
          console.log(res.result,"成功")
          const url = res.result; // 获取二维码内容
          // if(errMsg)
          let tmpinfo = res.result
          let tmplist
          tmplist = tmpinfo.split('?id=')
          console.log(tmplist,"11111111111")
          if(tmplist[1] === undefined || tmplist[1] === null){
            console.log("AAAAAAAAAAAAA")
            wx.showToast({
              title: '二维码不符合规则',
              icon: 'none',
              duration: 1000  // 2秒后自动关闭
            })
            return false
          }
          console.log(tmplist[1],"tmplist[1]")
          let tmpid = tmplist[1]
          wx.navigateTo({
            url: `/pages/flow_chart/flow_chart?id=${tmpid}`
          });
        },
        fail: (err) => {
          console.log(err,"失败")
          wx.showToast({ title: '扫码失败', icon: 'none' });
        }
      });
    }
})

